package com.ccjq.jq173.shosestore.web.controller.member;

import com.ccjq.jq173.shosestore.member.po.User;
import com.ccjq.jq173.shosestore.member.service.UserService;
import com.ccjq.jq173.shosestore.member.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class UserServlet extends HttpServlet {  //跳转用的
    private UserService userService;

    private static final String FLAG_REG ="reg";//注册页面
    private static final String FLAG_REG_SUBMIT ="rs";//提交注册

    private static final String FLAG_LOGIN ="login";//登陆页面
    private static final String FLAG_LOGIN_SUBMIT="ls";//提交登陆

    private static final String FLAG_INFO="info";//用户中心页面
    private static final String FLAG_LOGOUT="logout";//注销

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String flag=request.getParameter("flag");
        userService=new UserServiceImpl();
        if(FLAG_LOGIN.equals(flag))  //登录
        {
            toLoginPage(request,response);
        }
        else if(FLAG_REG.equals(flag)) //注册
        {
            toRegisterPage(request,response);
        }
        else if(FLAG_REG_SUBMIT.equals(flag)) //提交注册
        {
            regist(request,response);
        }
        else if(FLAG_INFO.equals(flag)) //用户中心页面
        {
            toUserInfoPage(request,response);
        }
        else if(FLAG_LOGOUT.equals(flag)) //注销
        {
            logout(request,response);
        }
        else if(FLAG_LOGIN_SUBMIT.equals(flag)) //登录提交
        {
            login(request,response);
        }
    }

    /**
     * 跳转到登陆页面
     */
    public void toLoginPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
        request.getRequestDispatcher("/WEB-INF/jsp/member/login.jsp").forward(request,response);
    }

    /*
        跳转到注册页面
     */
    private void toRegisterPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
        request.getRequestDispatcher("/WEB-INF/jsp/member/regist.jsp").forward(request,response);
    }

    private void regist(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
        String username=request.getParameter("username");
        String password=request.getParameter("password");
        String mobilePhone=request.getParameter("phone");
        String vicode=request.getParameter("vicode");
        String checkcode=(String)request.getSession().getAttribute("checkcode");
        if(checkcode.equalsIgnoreCase(vicode))
        {

            int result=userService.regist(username,password,mobilePhone);
            if(result == 1)
            {
                //跳转到用户中心页面
                User user=userService.loadUser(username);
                request.getSession().setAttribute("user",user);
                request.getRequestDispatcher("/WEB-INF/jsp/member/userinfo.jsp").forward(request,response);
            }
            else
            {
                request.setAttribute("message","用户注册失败");
                //返回到注册页面
                request.getRequestDispatcher("/WEB-INF/jsp/member/regist.jsp").forward(request,response);
            }
        }
        else
        {
            request.setAttribute("message","验证码不正确");
            //返回到注册页面
            request.getRequestDispatcher("/WEB-INF/jsp/member/regist.jsp").forward(request,response);
        }
    }
    /*
        用户中心页面
     */
    private void toUserInfoPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String usId=request.getParameter("userId");
        User user=userService.loadUserById(usId);
        request.setAttribute("user",user);
        request.getRequestDispatcher("/WEB-INF/jsp/member/userinfo.jsp").forward(request,response);
    }
    /*
    注销用户
     */
    private void logout(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.getSession().setAttribute("user",null);
        request.setAttribute("user",null);
        request.getRequestDispatcher("/WEB-INF/jsp/member/login.jsp").forward(request,response);
    }
    /*
    执行登陆
     */
    private void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username=request.getParameter("username");
        String password=request.getParameter("password");
        String vicode=request.getParameter("vicode");

        String checkcode=(String) request.getSession().getAttribute("checkcode");
        if(checkcode.equalsIgnoreCase(vicode))
        {
            User user=userService.login(username,password);
            if(user !=null)
            {
                request.getSession().setAttribute("user",user);
                request.getRequestDispatcher("/WEB-INF/jsp/home/home.jsp").forward(request,response);
            }
            else
            {
                request.setAttribute("message","用户登陆失败");
                request.getRequestDispatcher("/WEB-INF/jsp/member/login.jsp").forward(request,response);
            }

        }
        else
        {
            request.setAttribute("message","验证码不正确");
            request.getRequestDispatcher("/WEB-INF/jsp/member/login.jsp").forward(request,response);
        }

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }


}
